// page/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      baseURL:wx.baseURL,
      banners:[],
      indicatorDots: true,
      vertical: false,
      autoplay: true,
      interval: 2000,
      duration: 500,   
      tab:[],
      guanggao:[],
      pageNum:1,
      total:0,
      news:[],
      newsIndex:0,
      newsItems:'transform: translate(0rpx, 0rpx); transition: transform 0.3s;',
      loading:'数据加载中'
  },
  getBanners(){
      return new Promise((resolve,reject) => {
        wx.get('/index/banners').then((res) => {
            this.setData({
                banners:res
            }) 
            resolve()
        })
      })
     
  },
  getTab(){
    return new Promise((resolve,reject) => {
      wx.get('/index/categorys').then((res) => {
          this.setData({
              tab:res
          }) 
          resolve()
      })
  })
},
  getGuanggao(){
      return new Promise((resolve,reject) => {
          this.setData({loading:'数据加载中'})
          wx.get('/index/recommends?pageNum=' + this.data.pageNum).then(res => {
            this.setData({
                loading:'已经到底了哦~',
                guanggao:this.data.guanggao.concat(res.rows),
                total:res.total
            })
            resolve()
            })
      })
      
  },
  getNews(){
    return new Promise((resolve,reject) => {
      wx.get('/index/news').then((res) => {
          this.setData({
              news:res
          }) 
          this.changeNewsIndex()
          resolve()
      })
    })
  },
  changeNewsIndex(){
      setInterval(()=>{
          let newsIndex = this.data.newsIndex
          newsIndex ++
          this.setData({
              newsIndex,
              newsItems:'transform: translate(0rpx, -' + newsIndex * 80 +'rpx); transition: transform 0.3s;'
          })
          if(this.data.newsIndex == 2){
            setTimeout(() => {
                this.setData({
                    newsIndex:0,
                    newsItems:'transform: translate(0rpx,0rpx)'
                })
            },500)
        }
      },3000)
  },

  getList(){
    Promise.all([this.getNews(),this.getGuanggao(),this.getTab(),this.getBanners()]).then(() => {
        console.log('所有数据加载完成')
    }).catch(e => {
        console.log(e)
    })  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
     this.getList()
  },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
          url: '../logs/logs'
        })
      },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.data.pageNum = 1
      this.data.guanggao = []
      this.getList()
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.total > this.data.guanggao.length){
          this.data.pageNum ++
          this.getGuanggao()
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})